import {Component, OnInit, Input, EventEmitter, Output} from '@angular/core';
import { Trip } from '../../trip/trip';

import { ActivatedRoute, Params } from '@angular/router';
import { TripService } from '../../trip/trip.service';

import { CarrierService } from '../../carrier/carrier.service';
import { Carrier } from '../../carrier/carrier';

import { DriverService } from '../../driver/driver.service';
import { Driver } from '../../driver/driver';

import { BusService } from '../../bus/bus.service';
import { Bus } from '../../bus/bus';

@Component({
    selector: 'tripcarrier-detail',
    templateUrl: './tripcarrier-detail.component.html',
    // styleUrls: ['./trip-detail.component.css']
})

export class TripcarrierDetailComponent implements OnInit {
    @Input()
    trip: Trip;
  @Output() onVoted = new EventEmitter<boolean>();
    buses: Bus[];
    drivers: Driver[];
    carriers: Carrier[];

    constructor(
        private tripService: TripService,

        private carrierService: CarrierService,
        private busService: BusService,
        private driverService: DriverService,

        private router: ActivatedRoute) {

    }

    ngOnInit() {
        this.router.params.forEach((params: Params) => {
            let trip_id = +params['trip_id'];
            this.tripService.getTrip(trip_id).then(trip => this.trip = trip);

            this.carrierService.getCarriers().then(carriers => this.carriers = carriers);
            this.busService.getBuss().then(buses => this.buses = buses);
            this.driverService.getDrivers().then(drivers => this.drivers = drivers);

        });
    }

    goBack() {
        window.history.back();
    }

    save(): void {
        this.tripService.save(this.trip).then(() => this.cancel());
    }

    delete(): void {
        this.tripService.delete(this.trip).then(() => this.cancel());
    }

    cancel(): void {
        this.trip = null;
      this.onVoted.emit();
    }
}
